//
//  MXFindSearchVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/18.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 专题的收缩页
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXFindSearchVC.h"
#import "MXFindSearchCollectionViewCell.h"
#import "MXFindSearchDetailVC.h"
#import "MXFindSearchDetailVC.h"
@interface MXFindSearchVC ()<UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UITextField *searchTopTextField;

@property (nonatomic, retain) UIButton *searchTopCancelButton;

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) UITextField *searchBar;

@property (nonatomic, retain) NSArray *array;

@end

@implementation MXFindSearchVC

- (void)dealloc
{
    [_searchBar release];
    [_array release];
    [_collectionView release];
    [_searchTopCancelButton release];
    [_searchTopTextField release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 全局数组初始化
    self.array = [NSArray array];
    
    self.array = [NSArray arrayWithObjects:@"青春", @"都市", @"休闲", @"商务", @"穿搭", @"配色", @"情侣", @"英伦", @"复古", @"日韩", @"设计师", @"小清新", @"运动", nil];
    
    
#pragma mark - 建立searchBar
    self.searchBar = [[[UITextField alloc] initWithFrame:CGRectMake(WIDTHPRO * 25,HEIGHTPRO * 30, WIDTH - HEIGHTPRO * 110,WIDTHPRO * 35)] autorelease];
    self.searchBar.backgroundColor = [UIColor whiteColor];
    
    self.searchBar.layer.cornerRadius = 8;
    
    self.searchBar.delegate = self;
    
    self.searchBar.clearButtonMode = UITextFieldViewModeAlways;
    
    UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(0,HEIGHTPRO * 5,WIDTHPRO * 25,HEIGHTPRO * 25)] autorelease];
    
    img.backgroundColor = [UIColor clearColor];
    
    img.image = [UIImage imageNamed:@"search_red"];
        
    self.searchBar.leftView = img;
    
    self.searchBar.leftViewMode = UITextFieldViewModeAlways;
    
    self.searchBar.placeholder = @"请输入感兴趣的内容";
    
    self.navigationItem.titleView = self.searchBar;
    
    UIButton *left = [UIButton buttonWithType:(UIButtonTypeCustom)];
    left.frame = CGRectMake(0, 0, WIDTHPRO * 25, HEIGHTPRO * 25);
    [left addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    [left setBackgroundImage:[UIImage imageNamed:@"search_back_red"] forState:(UIControlStateNormal)];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:left];
    
    self.navigationItem.leftBarButtonItem = leftButton;
        
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:(UIBarButtonItemStylePlain) target:self action:@selector(searchAction)];
    
// 设置导航文字颜色
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    
    
// 隐藏TabBar
    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationController.navigationBarHidden = NO;
    
    
    [self creatCollectionView];
//    [self createTextFieldAndButton];
    
    // Do any additional setup after loading the view.
}

#pragma mark - 创建CollectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 100, HEIGHTPRO * 40);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTHPRO * 30 , HEIGHTPRO * 10 , WIDTHPRO * 30, HEIGHTPRO * 10);
#pragma mark - 根据TopView手动更改
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT  - HEIGHTPRO * 49) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[MXFindSearchCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor cyanColor];
    
    cell.naviSearch = self.navigationController;
    
    [cell getInfo:self.array[indexPath.row]];
    return cell;
}

#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchAction
{
// push 搜索
    MXFindSearchDetailVC *detailVC = [[[MXFindSearchDetailVC alloc] init] autorelease];
    
    detailVC.searchResult = self.searchBar.text;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
